<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="<%=basePath%>resources/css/bootstrap.min.css" rel="stylesheet"/>
    <title>项目脚手架</title><!--TODO project-->

    <style type="text/css">
        .form-signin {
            width: 300px;
            margin: 50px auto 0;
        }
        input {
            margin-bottom: 10px;
        }
        button {
            width: 48%;
        }
    </style>
</head>
<body>

<div class="container">
    <form class="form-signin">
        <h2 class="form-signin-heading">请登录</h2>

        <label for="inputUsername" class="sr-only">用户名</label>
        <input type="text" id="inputUsername" class="form-control" placeholder="用户名" required="" autofocus="" autocomplete="off">

        <label for="inputPassword" class="sr-only">密码</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="密码" required="" autocomplete="off">

        <button class="btn btn-info loginBtn" type="submit">登录</button>
        <button class="btn btn-primary pull-right registerBtn" type="submit">注册</button>
    </form>
</div> <!-- /container -->

<script src="<%=basePath%>resources/js/jquery-2.1.0.min.js"></script>
<script src="<%=basePath%>resources/js/bootstrap.min.js"></script>
<script>
$(function () {
    $(".loginBtn").click(function (e) {
        $(".loginBtn").attr("disabled", true);
        e.preventDefault();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "<%=basePath%>user/login",
            data: JSON.stringify({
                username: $("#inputUsername").val(),
                password: $("#inputPassword").val()
            }),
            dataType: 'json'
        })
        .done(function (resp) {
            console.info(resp);
            location.href = "./tweet/";
        })
        .fail(function (e) {
            alert("请求失败!");
        })
        .always(function () {
            $(".loginBtn").attr("disabled", false);
        });
    });

    $(".registerBtn").click(function (e) {
        $(".registerBtn").attr("disabled", true);
        e.preventDefault();
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "<%=basePath%>user/register",
            data: JSON.stringify({
                username: $("#inputUsername").val(),
                password: $("#inputPassword").val()
            }),
            dataType: 'json'
        })
        .done(function (resp) {
            console.info(resp);
            alert(resp.msg);
        })
        .fail(function (e) {
            alert("请求失败!");
        })
        .always(function () {
            $(".registerBtn").attr("disabled", false);
        });
    });
});
</script>
</body>
</html>
